package com.mathcat.mobile.android.net;

import java.util.concurrent.ExecutorService;

import org.apache.http.HttpResponse;

public class AsyncHttpCaller implements HttpCaller {

	private HttpCaller delegate;
	
	private ExecutorService executorService;
	
	public AsyncHttpCaller(HttpCaller delegate, ExecutorService executorService) {
		if (executorService == null)
			throw new NullPointerException("ExecutorService must not be null!");
		
		this.delegate = delegate;
		this.executorService = executorService;
	}
	
	@Override
	public void onCompelete(HttpResponse response) {
		if (this.delegate == null)
			return;
		
		final HttpResponse resp = response;
		Runnable r = new Runnable() {
			@Override
			public void run() {
				delegate.onCompelete(resp);
			}
		};
		
		this.executorService.submit(r);
	}

	@Override
	public void onFailed(int errorCode) {
		if (this.delegate == null)
			return;
		
		final int ec = errorCode;
		Runnable r = new Runnable() {
			@Override
			public void run() {
				delegate.onFailed(ec);
			}
		};
		this.executorService.submit(r);
	}

}
